package middlewave

import (
	"backend/constants/errorCode"
	"backend/constants/usertype"
	"backend/models"
	"backend/service/common"
	"backend/utils"
	"github.com/beego/beego/v2/server/web/context"
	"strings"
)

func CheckAccess(ctx *context.Context) {
	flg := false
	if !strings.Contains( ctx.Request.RequestURI,"/user/login"){
		username := common.GetUserId(ctx)
		userType := models.GetUserType(username)
		if userType == usertype.ADMIN || userType == usertype.MANAGER{
			if  userType == usertype.MANAGER {
				if strings.Contains(ctx.Request.RequestURI,"/user/delete")||strings.Contains(ctx.Request.RequestURI,"/user/add"){
					flg = true
				}
			}
		}else{
			flg = true
		}
	}
	if flg {
		result := errorCode.ErrUserAccess
		str := utils.Strval(result)
		ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
		ctx.WriteString(str)
		return
	}
}